<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Article {

	public function start() {
		global $db,$cache,$request;

		$action = $request->get( 'p' );
		$id = $request->id;

		switch( $action ) {
			case 'add':
				$this->article_add();
				break;
			case 'edit':
				$this->article_add( 'edit' );
				break;
			case 'saveadd':
				// 执行保存日志
				$article_id = $this->add();
				if( $article_id == 0 ) {
					break;
				}
				//$article_type = $request->get( 'article_type', 'P' );
				if( $cache->config['arttype'] == '静态' ){
					page_make( $article_id, $article_type );
				}
				$cache->refresh( 'count', TRUE );
				$cache->refresh( 'article_new', TRUE );
				$cache->refresh( 'article_list', TRUE );
				echo '<p class="warning">' . L('_ADD_ARTICLE_SUCCESS_') . '</p>';
				break;
			case 'saveedit':
				// 执行修改日志
				if( $this->edit( $id ) == 0 ) {
					break;
				}
				$article_type = $request->get('article_type','P');
				if( $cache->config['arttype'] == '静态' ){
					page_make( $article_id, $article_type );
				}
				$cache->refresh( 'article_hot', TRUE );
				$cache->refresh( 'article_new', TRUE );
				$cache->refresh( 'article_list', TRUE );
				echo '<p class="warning">' . L('_EDIT_ARTICLE_SUCCESS_') . '</p>';
				break;
			case 'top':
				// 执行日志置顶
				$db->query( "UPDATE `" . DB_PREFIX . "article` SET istop = 1 WHERE id=$id" );
				// 删除缓存
				$cache->refresh( 'article_list', TRUE );
				echo '<p class="warning">' . '日志置顶成功' . '</p>';
				break;
			case 'topcancel':
				// 执行取消日志置顶
				$db->query( "UPDATE `" . DB_PREFIX . "article` SET istop = 0 WHERE id=$id" );
				// 删除缓存
				$cache->refresh( 'article_list', TRUE );
				echo '<p class="warning">' . '取消日志置顶成功' . '</p>';
				break;
			case 'reflash':
				// 执行刷新日志
				/*page_make_html( $id );
				$db->query( "UPDATE `" . DB_PREFIX . "article` SET `update`=0 WHERE id=$id" );*/
				echo '<p class="warning">' . '刷新日志成功' . '</p>';
				break;
			case 'del':
				// 执行删除日志
				$category = $db->result( "SELECT category FROM `" . DB_PREFIX . "article` WHERE id=$id AND isdel = 0" );
				if( $category == '' ) {
					$theme->show_message( '日志不存在！' );
					break;
				}
				// 分类计数减一
				$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum = articlenum - 1 WHERE cid=$category" );
				// 删除此日志的评论
				$db->query( "UPDATE `" . DB_PREFIX . "comment` SET isdel = 1 WHERE articleid=$id" );
				// 删除对应标签
				$tags = $db->result( "SELECT tag FROM `" . DB_PREFIX . "article` WHERE id=$id" );
				$tags = str_replace( '*}{*',',',$tags );
				$tags = str_replace( '*}','',$tags );
				$tags = str_replace( '{*','',$tags );
				$tags = split( ',' , $tags );
				foreach($tags as $tag){
					if( $tag != '' ){
						$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num=t_num-1 WHERE t_id=$tag" );
					}
				}
				// 删除日志
				$db->query( "UPDATE `" . DB_PREFIX . "article` SET isdel = 1 WHERE id=$id" );
				// 删除缓存
				$cache->article_new = NULL;
				$cache->article_hot = NULL;
				$cache->comment_new = NULL;
				$cache->category = NULL;
				$cache->count = NULL;
				$cache->tag = NULL;
				echo '<p class="warning">' . '删除日志成功' . '</p>';
				break;
			case 'addreply':
				// 执行添加回复
				$reply = htmlspecialchars( $request->get('commentreply','P') );
				$reply = str_replace( array("\r\n", "\n", "\r"), '<br />', $reply );
				$db->query( "UPDATE `" . DB_PREFIX . "comment` SET replyuser = '" . $_SESSION['user_name'] . "',reply = '$reply',replytime = " . PHP_TIME . " WHERE cid = $id" );
				echo '<p class="warning">' . '添加回复成功' . '</p>';
				break;
			case 'delreply':
				// 执行删除回复
				$db->query( "UPDATE `" . DB_PREFIX . "comment` SET replyuser = NULL WHERE cid = $id" );
				echo '<p class="warning">' . '删除回复成功' . '</p>';
				break;
			case 'delcomment':
				// 执行删除评论
				$aid = $db->result( "SELECT articleid FROM `" . DB_PREFIX . "comment` WHERE cid=$id AND isdel = 0" );
				if( $aid == '' ) {
					echo '<p class="error">' . '评论不存在！' . '</p>';
					break;
				}
				$db->query( "UPDATE `" . DB_PREFIX . "article` SET comment=comment-1 WHERE id=$aid" );
				$db->query( "UPDATE `" . DB_PREFIX . "comment` SET isdel=1 WHERE cid=$id" );
				$cache->article_hot = NULL;
				$cache->comment_new = NULL;
				$cache->guestbook_new = NULL;
				$cache->count = NULL;
				echo '<p class="warning">' . '删除评论成功' . '</p>';
				break;
			case 'censor':
				// 评论审核通过
				$db->query( "UPDATE `" . DB_PREFIX . "comment` SET isshow = 1 WHERE cid = $id" );
				$cache->comment_new = NULL;
				$cache->guestbook_new = NULL;
				echo '<p class="warning">' . '审核评论成功' . '</p>';
				break;
			case 'MakearticlePage':
			    $this->make_article_page();
				break;
			case 'datacheckexe':
			    $this->data_check_exe();
				break;
			case 'tagdatacheckexe':
				$this->tag_data_check_exe();
				break;
			case 'batch':
				$this->batch();
				break;
			default:
				$this->show( $action );
		}
	}

	private function make_article_page() {
		global $db,$_CACHE;
		$_CACHE['count'][0]['online'] = cache_online();
		if( count( $_CACHE['category'] ) == 0 ) {
			show_admin_message( '尚无文章分类', 'admin_article.php' );
			exit;
		}else{
			foreach( $_CACHE['category'] as $category ) {
				$file_path = XKLOG_ROOT . 'article/' . $category['alias'] . '/';
				if ( !is_dir( $file_path ) ) {
					@mkdir( $file_path, 0777 );
				}
				$article_arr = array();
				$article_arr = $db->fetch_all( "SELECT id FROM `" . DB_PREFIX . "article` WHERE isdel=0 AND category=" . $category['cid'] . " AND type='静态' ORDER BY id DESC" );
				foreach( $article_arr as $article ) {
					page_make_html( $article['id'] );
					$db->query( "UPDATE `" . DB_PREFIX . "article` SET `update`=0 WHERE id=" . $article['id'] );
				}
			}
		}
		show_admin_message( '所有静态页已经重新生成', 'admin_article.php' );
		exit;
	}

	private function data_check_exe() {
		global $db,$cache;
		$article_arr = $db->fetch_all( "SELECT id FROM `" . DB_PREFIX . "article`" );
		if( count( $article_arr ) == 0 ) {
			$this->show( 'datacheck', '没有日志你修正个啥呀？', 'error' );
			return;
		}
		foreach( $article_arr as $article ) {
			$temp = $db->result( "SELECT COUNT(cid) FROM `" . DB_PREFIX . "comment` WHERE isdel=0 AND articleid=" . $article['id'] );
			 $db->query( "UPDATE `" . DB_PREFIX . "article` SET comment=" . $temp . " WHERE id=" . $article['id'] );
		}
		$cache->refresh( 'article_hot', TRUE );
		$this->show( 'datacheck', '日志的评论数已经重新统计', 'warning' );
	}

	private function tag_data_check_exe() {
		global $db,$cache;
		$article_arr = $db->fetch_all( "SELECT tag FROM `" . DB_PREFIX . "article` WHERE isdel=0" );
		$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num=0" );
		$tag_arr = $db->fetch_all( "SELECT t_id,t_name FROM `" . DB_PREFIX . "tags`" );
		if( count( $article_arr ) == 0 ) {
			$this->show( 'datacheck', '没有日志你修正个啥呀？', 'error' );
			return;
		}
		foreach( $article_arr as $article ) {
			$article['tag'] = str_replace( '*}{*',',',$article['tag'] );
			$article['tag'] = substr( $article['tag'],2 );
			$article['tag'] = substr( $article['tag'],0,-2 );
			$article['tag'] = explode( ',' , $article['tag'] );
			foreach( $article['tag'] as $tag ) {
				foreach( $tag_arr as $tag_old ) {
					if( $tag == $tag_old['t_id'] ) {
						$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num=t_num+1 WHERE t_id=" . $tag_old['t_id'] );
						break;
					}
				}
			}
		}
		if( $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=1" ) != 0 ) {
			$this->show( 'datacheck', '标签数据已经重新统计。如欲删除日志数为 0 的标签，请先清空回收站中的日志。', 'warning' );
		}else{
			$db->query( "DELETE FROM `" . DB_PREFIX . "tags` WHERE t_num=0" );
			$this->show( 'datacheck', '标签数据已经重新统计', 'warning' );
		}
		$cache->refresh( 'tag', TRUE );
	}

	private function batch() {
		global $cache,$db;
		$control = getGP( 'Control','P' );
		$id_arr = array();
		$id_arr = getGP( 'id_arr','P','array' );
		$size = count( $id_arr );
		if( $size == 0 ) {
			echo '<p class="error">' . '您没有选中任何日志' . '</p>';
			return;
		}
		$sql = '';
		for( $i=0; $i<$size; $i ++ ){
			$sql .= 'id=' . $id_arr[$i] . ' OR ';
		}
		$sql = substr( $sql,0,-4 );
		if( $control == '删除' ) {
			// 删除日志
			$db->query( 'UPDATE `' . DB_PREFIX . 'article` SET isdel=1 WHERE (' . $sql . ')' );
			// 删除对应标签
			$tag_arr = $db->fetch_all( "SELECT tag FROM `" . DB_PREFIX . "article` WHERE ( $sql )" );
			foreach( $tag_arr as $tags ) {
				$tags['tag'] = str_replace( '*}{*',',',$tags['tag'] );
				$tags['tag'] = str_replace( '*}','',$tags['tag'] );
				$tags['tag'] = str_replace( '{*','',$tags['tag'] );
				$tags['tag'] = split( ',' , $tags['tag'] );
				foreach($tags['tag'] as $tag){
					if( $tag != '' ){
						$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num=t_num-1 WHERE t_id=$tag" );
					}
				}
			}
			// 删除对应评论
			$id_arr = str_replace( 'id=','',$sql );
			$id_arr = split( ' OR ',$id_arr );
			$size = count( $id_arr );
			$sql = '';
			for( $i=0; $i<$size; $i ++ ){
				$sql .= 'articleid=' . $id_arr[$i] . ' OR ';
			}
			$sql = substr( $sql,0,-4 );
			$db->query( "UPDATE `" . DB_PREFIX . "comment` SET isdel = 1 WHERE ( $sql )" );
			// 重新统计分类数据
			foreach( $_CACHE['category'] as $category ) {
				$cid = $category['cid'];
				$article_num = $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=0 AND category=$cid" );
				if( $category['articlenum'] != $article_num ) {
					$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum=$article_num WHERE cid=$cid" );
				}
			}
			// 刷新缓存
			cache_article_hot();
			cache_article_new();
			cache_comment_new();
			cache_category();
			cache_count();
			cache_tag();
			show_admin_message( '批量删除日志成功', 'admin_article.php?action=article' );
		}elseif( $control == '移动' ) {
			$category = getGP( 'article_category','P','int' );
			$sql = 'UPDATE `' . DB_PREFIX . 'article` SET category=' . $category . ' WHERE (' . $sql . ')';
			$db->query( $sql );
			// 重新统计分类数据
			foreach( $_CACHE['category'] as $category ) {
				$cid = $category['cid'];
				$article_num = $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=0 AND category=$cid" );
				if( $category['articlenum'] != $article_num ) {
					$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum=$article_num WHERE cid=$cid" );
				}
			}
			// 刷新缓存
			cache_article_hot();
			cache_article_new();
			cache_category();
			show_admin_message( '批量移动日志成功，您可能需要重新生成静态页', 'admin_article.php?action=article' );
		}elseif( $control == '转换' ) {
			$type = getGP( 'article_type','P' );
			$sql2 = "UPDATE `" . DB_PREFIX . "article` SET type='$type' WHERE ( $sql )";
			$db->query( $sql2 );
			// 重新生成静态与半静态页
			if( $type != '动态' ) {
				$_CACHE['count'][0]['online'] = cache_online();
				foreach( $_CACHE['category'] as $category ) {
					$file_path = XKLOG_ROOT . 'article/' . $category['alias'] . '/';
					if ( !is_dir( $file_path ) ) {
						@mkdir( $file_path, 0777 );
					}
					$article_arr = array();
					$article_arr = $db->fetch_all( "SELECT id FROM `" . DB_PREFIX . "article` WHERE ( $sql )" );
					foreach( $article_arr as $article ) {
						page_make( $article['id'], $type );
						$db->query( "UPDATE `" . DB_PREFIX . "article` SET `update`=0 WHERE id=" . $article['id'] );
					}
				}
			}
			// 刷新缓存
			cache_article_hot();
			cache_article_new();
			show_admin_message( '批量转换日志成功', 'admin_article.php?action=article' );
		}else{
			show_admin_message( '未定义操作', 'admin_article.php' );
		}
	}

	// 添加一篇日志
	private function add(){
		global $cache,$db,$request;
		$title       = $request->get('article_title','P');
		$keywords    = $request->get('article_keywords','P');
		$description = $request->get('article_description','P');
		$content     = $request->get('article_content','P');
		$summary     = $request->get('article_summary','P');
		$address     = $request->get('article_alias','P');
		$type        = $request->get('article_type','P');
		if( trim( $title ) == '' || trim( $content ) == '' ){
			echo '<p class="error">' . '错误：您没有输入标题或正文！' . '</p>';
			return 0;
		}
		if( strlen( $content ) > $cache->config['artmaxlength'] ){
			echo '<p class="error">' . '错误：日志正文不可超过 ' . $cache->config['artmaxlength'] . ' 字节！' . '</p>';
			return 0;
		}
		if( strlen( $summary ) > $cache->config['artmaxlength'] ){
			echo '<p class="error">' . '错误：日志摘要不可超过 ' . $cache->config['artmaxlength'] . ' 字节！' . '</p>';
			return 0;
		}
		// 日志别名检查，防止重复
		// 不能为纯数字，防止和系统默认别名重复哦（使用 is_numeric 可能有误伤，不过算了）
		if( $address != '' ) {
			if( is_numeric( $address ) ) {
				echo '<p class="error">' . '错误：日志别名不可以为纯数字！' . '</p>';
				return 0;
			}
			if( $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE address='$address'") ) {
				echo '<p class="error">' . '错误：日志别名已经存在！' . '</p>';
				return 0;
			}
			$file_path = APP_CACHE . $address . '.html';
			@file_put_contents( $file_path, ' ' );
			if( file_exists( $file_path ) ) {
				unlink( $file_path );
			}else{
				echo '<p class="error">' . '错误：日志别名不合法！' . '</p>';
				return 0;
			}
			unset( $file_path );
		}
		// 处理分类
		$category = $request->get('article_category','P');
		$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum = articlenum + 1 WHERE cid = '$category'");
		$cache->refresh( 'category', TRUE );

		$type = $request->get('article_type','P');
		$trackback = $request->get('article_trackback','P');

		// 处理标签
		$temp = explode( ',' , $request->get('article_tag','P') );
		$temp = array_unique( $temp );
		$tag = '';
		$find = false;
		foreach( $temp as $itag ){
			if( $itag != '' ){
				foreach( $cache->tag as $tag_id => $tag_arr ){
					if( $tag_arr['t_name'] == $itag ){
						$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num = t_num + 1 WHERE t_name = '$itag'");
						$tag .= '{*' . $tag_id . '*}';
						$find = true;
						break;
					}
				}
				if( $find == false ){
					$db->query( "INSERT INTO `" . DB_PREFIX . "tags` (`t_name`,`t_num`) VALUES ('$itag',1)" );
					$tag .= '{*' . $db->insert_id() . '*}';
				}else{
					$find = false;
				}
			}
		}
		$cache->refresh( 'tag', TRUE );

		if( !isset( $_SESSION['upfile'] ) ) $_SESSION['upfile'] = '';
		$db->query( "INSERT INTO `" . DB_PREFIX . "article` (`title`,`keywords`,`description`,`time`,`author`,`category`,`tag`,`summary`,`content`,`read`,`comment`,`address`,`type`,`attachment`) VALUES ('$title', '$keywords', '$description', '" . PHP_TIME . "', '" . $request->get( APP_PREFIX . 'username', 'C' ) . "', $category, '$tag', '$summary', '$content',0,0,'$address','$type','" . $_SESSION['upfile'] . "')" );
		unset($_SESSION['upfile']);
		return $db->insert_id();
	}

	// 修改一篇日志
	private function edit( $article_id ){
		global $cache,$db,$request;
		$title = $request->get('article_title','P');
		$keywords = $request->get('article_keywords','P');
		$description = $request->get('article_description','P');
		$content = $request->get('article_content','P');
		$summary = $request->get('article_summary','P');
		$address = trim( $request->get('article_alias','P') );
		$type = $request->get('article_type','P');
		if( trim( $title ) == '' || trim( $content ) == '' ){
			echo '<p class="error">' . '错误：您没有输入标题或正文！' . '</p>';
			return 0;
		}
		if( strlen( $content ) > $cache->config['artmaxlength'] ){
			echo '<p class="error">' . '错误：日志正文不可超过 ' . $cache->config['artmaxlength'] . ' 字节！' . '</p>';
			return 0;
		}
		if( strlen( $summary ) > $cache->config['artmaxlength'] ){
			echo '<p class="error">' . '错误：日志摘要不可超过 ' . $cache->config['artmaxlength'] . ' 字节！' . '</p>';
			return 0;
		}
		// 检查文章是否存在
		if( $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE id={$article_id} AND isdel=0 AND type<3" ) == 0 ){
			echo '<p class="error">' . '哪有这篇文章？您来找找看？' . '</p>';
			return 0;
		}
		if( $address != '' ) {
			// 日志别名检查，防止重复
			if( is_numeric( $address ) ) {
				echo '<p class="error">' . '错误：日志别名不可以为纯数字！' . '</p>';
				return 0;
			}
			if( $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE address='$address' AND id<>$article_id") ) {
				echo '<p class="error">' . '错误：日志别名重复！' . '</p>';
				return 0;
			}
			$file_path = APP_CACHE . $address . '.html';
			@file_put_contents( $file_path, ' ' );
			if( file_exists( $file_path ) ) {
				unlink( $file_path );
			}else{
				echo '<p class="error">' . '错误：日志别名不合法！' . $file_path . '</p>';
				return 0;
			}
			unset( $file_path );
		}
		// 清理旧分类
		$category_old = $db->result( "SELECT category FROM `" . DB_PREFIX . "article` WHERE id = $article_id" );
		$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum = articlenum - 1 WHERE cid = $category_old");
		// 处理新分类
		$category = $request->get('article_category','P');
		$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum = articlenum + 1 WHERE cid = $category");
		$cache->refresh( 'category', TRUE );

		$type = $request->get('article_type','P');
		$trackback = $request->get('article_trackback','P');

		// 清理旧标签
		$tags_old = $db->result( "SELECT tag FROM `" . DB_PREFIX . "article` WHERE id = $article_id" );
		$tags_old = str_replace( '*}{*',',',$tags_old);
		$tags_old = str_replace( '*}','',$tags_old);
		$tags_old = str_replace( '{*','',$tags_old);
		$tags_old = explode( ',' ,$tags_old );
		foreach( $tags_old as $i_tag ){
			if( $i_tag != '' ){
				$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num = t_num - 1 WHERE t_id = $i_tag" );
			}
		}

		// 处理新标签
		$temp = explode( ',' , $request->get('article_tag','P') );
		$temp = array_unique( $temp );
		$tag = '';
		$find = false;
		$tag_list = $db->fetch_all( 'SELECT * FROM `' . DB_PREFIX . 'tags` ORDER BY t_id Asc' );
		foreach( $temp as $itag ){
			if( $itag != '' ){
				foreach( $tag_list as $tag_arr ){
					if( $tag_arr['t_name'] == $itag ){
						$db->query( "UPDATE `" . DB_PREFIX . "tags` SET t_num = t_num + 1 WHERE t_name = '$itag'");
						$tag .= '{*' . $tag_arr['t_id'] . '*}';
						$find = true;
						break;
					}
				}
				if( $find == false ){
					$db->query( "INSERT INTO `" . DB_PREFIX . "tags` (`t_name`,`t_num`) VALUES ('$itag',1)" );
					$tag .= '{*' . $db->insert_id() . '*}';
				}else{
					$find = false;
				}
			}
		}
		$cache->refresh( 'tag', TRUE );

		if( !isset( $_SESSION['upfile'] ) ) $_SESSION['upfile'] = '';
		$upfile = $db->result( "SELECT attachment FROM `" . DB_PREFIX . "article` WHERE id = $article_id" );
		$upfile .= $_SESSION['upfile'];
		$db->query( "UPDATE `" . DB_PREFIX . "article` SET title='{$title}' ,keywords='{$keywords}' ,description='{$description}' ,category={$category} ,tag='{$tag}' ,summary='{$summary}' ,content='{$content}' ,address='{$address}' ,type='{$type}' ,attachment='{$upfile}' WHERE id={$article_id}" );
		unset($_SESSION['upfile']);
		return $article_id;
	}

	private function show( $action ) {
		@header("content-type: text/html; charset=utf-8");
?>
<div class="admin_panel">
	<?php
		switch( $action ) {
		case 'list':
			$this->article_list();
			break;
		case 'page':
			$this->page_control();
			break;
		case 'datacheck':
			$this->data_check();
			break;
		default:
			$this->main();
		}
	?>
</div>
<?php
	}

	private function article_add( $action = '' ) {
		global $cache;
		if( $action == 'edit' ) {
			global $db,$request;
			$article = $db->fetch_one_array( 'SELECT * FROM `' . DB_PREFIX . 'article` WHERE id=' . $request->id );
			$article['trackback'] = '';//$db->result( 'SELECT tb_url FROM `' . DB_PREFIX . 'trackback` WHERE tb_type = 0 AND tb_article_id=' . $id );
			// 处理附件
			$article['attachment'] = str_replace( '*}{*',',',$article['attachment']);
			$article['attachment'] = str_replace( '*}','',$article['attachment']);
			$article['attachment'] = str_replace( '{*','',$article['attachment']);
			$temp = array();
			$temp = split( ',' , $article['attachment'] );
			$article['attachment'] = '';
			$blog_path = str_replace( ' ','%20',APP_PATH );
			foreach($temp as $row){
				if( !is_numeric( $row ) ) continue;
				$file_info = $db->fetch_one_array( "SELECT ul_filetype,ul_filepath FROM `" . DB_PREFIX . "upload` WHERE ul_id=$row" );
				if ( stristr( $file_info['ul_filetype'],'image' ) ) {
					$ubb = '<img src=' . $blog_path . $file_info['ul_filepath'] . ' />';
					$ubb2 = '<img src=' . $blog_path . 'include/attachment.php?id=' . $row . ' />';
				} else {
					$ubb = '<span class=download><a href=' . $blog_path . $file_info['ul_filepath'] . ' target=_blank>点击这里下载</a></span>';
					$ubb2 = '<span class=download><a href=' . $blog_path . 'include/attachment.php?id=' . $row . ' target=_blank>点击这里下载</a></span>';
				}
				$article['attachment'] .= '<li><a href="javascript:;" onclick="to_editor(\''.$ubb2.'\')" title="隐藏附件路径">[防盗链插入]</a> <a href="#" onclick="to_editor(\''.$ubb.'\')" title="显示附件路径">[显式插入]</a> ' . $file_info['ul_filepath'] . '</li>';
			}
			$article['attachment'] = '<ol>' . $article['attachment'] . '</ol>';
			// 处理标签
			$article['tag'] = str_replace( '*}{*',',',$article['tag']);
			$article['tag'] = str_replace( '*}','',$article['tag']);
			$article['tag'] = str_replace( '{*','',$article['tag']);
			$temp = array();
			$temp = split( ',' , $article['tag']);
			$article['tag'] = '';
			foreach($temp as $row){
				if( isset( $cache->tag[$row]['t_name'] ) ){
					$article['tag'] .= '{*' . $cache->tag[$row]['t_name'] . '*}';
				}
			}
			$article['tag'] = str_replace( '*}{*',',',$article['tag']);
			$article['tag'] = str_replace( '*}','',$article['tag']);
			$article['tag'] = str_replace( '{*','',$article['tag']);
		} else {
			$article = array();
			$temp = array('title','content','summary','category','tag','trackback','address','keywords','description');
			foreach ( $temp as $value ) {
				$article[$value] = '';
			}
			$article['trackback'] = '此功能尚未完成';
		}
	// $article['p'], $article['extra'] );
?>

<style>
.input_submit { border:1px solid #a3a3a3; background-color: #e7e7e7; padding: 5px; width: 100px; font-weight:bold; }
.admin_content { margin:0px; padding:0px; margin-top:5px; }
.admin_content .input_text { border: 1px solid #c8c8c8; padding: 5px; width: 235px; font-size: 14px; }
.admin_title .tit_right { float: right; font-size: 12px; font-weight:normal; padding-top:2px; }
.admin_content select { border:1px solid #c8c8c8; padding: 5px; width: 245px; font-size: 14px; }
.admin_content .input_radio { margin:5px; }
.admin_title { cursor:pointer; }
</style>

<script type="text/javascript">
	KE.init({
		id : 'article_content',
		resizeMode : 1,
		width : '668px',
		allowFileManager : true,
		newlineTag : 'p'
	});
	KE.init({
		id : 'article_summary',
		resizeMode : 1,
		width : '668px',
		allowFileManager : true,
		newlineTag : 'p'
	});
	var flag_content=false,flag_summary=false;
</script>

<form method="post" action="index.php?m=article&p=<?php if( $action == 'edit' ) { echo 'saveedit&id=' . $request->id; } else { echo 'saveadd'; }?>" id="ajax_form">

<div style="float:left;width:680px;">

	<div class="admin_panel" onclick="if(!flag_content){flag_content=true;KE.create('article_content');KE.util.focus('article_content');}">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_content')">文章内容<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_content">
				<textarea id="article_content" name="article_content" style="width:665px;height:400px;border:1px solid #c8c8c8;"><?php echo $article['content'] ?></textarea>
			</div>
		</div>
	</div>

	<div class="admin_panel" onclick="if(!flag_summary){flag_summary=true;KE.create('article_summary');KE.util.focus('article_content');}">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_summary')">文章摘要<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_summary" style="display:none;">
				<textarea id="article_summary" name="article_summary" style="width:665px;height:200px;border:1px solid #c8c8c8;"><?php echo $article['summary'] ?></textarea>
			</div>
		</div>
	</div>

	<?php if( $action == 'edit' ) { ?>
	<div class="admin_panel" onmouseover="if(!flag_summary){flag_summary=true;KE.create('article_summary');KE.util.focus('article_content');}">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_upfile')">已有附件<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_upfile" style="text-align:left;">
				<?php echo $article['attachment'] ?>
			</div>
		</div>
		<script>
		function to_editor(id, html){
			KE.util.focus(id);
			KE.util.selection(id);
			KE.util.insertHtml(id, html);
		}
		</script>
	</div>
	<?php } ?>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_upload')">上传附件<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_upload" style="display:none;">
				<iframe frameborder="0" src="../include/upload.php" width="600" height="110"></iframe>
			</div>
		</div>
	</div>

</div>

<div style="float:right;width:260px;">

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_submit')">提交<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_submit" style="padding:10px;">
				<input type="submit" onclick="KE.util.setData('article_content');KE.util.setData('article_summary');" value=" 提  交 " class="input_submit" />
			</div>
		</div>
	</div>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_title')">标题<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_title">
				<input name="article_title" type="text" id="article_title" size="40" class="input_text" value="<?php echo $article['title'] ?>"/>
			</div>
		</div>
	</div>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_category')">分类<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_category">
				<select name="article_category" id="article_category">
				<?php
				global $cache;
				foreach ( $cache->category as $value ) {
					if( $article['category'] == $value['cid'] ){
						echo '<option value="' . $value['cid'] . '" selected="selected">' . $value['name'] . '</option>';
					}else{
						echo '<option value="' . $value['cid'] . '">' . $value['name'] . '</option>';
					}
				}
				?>
				</select>
			</div>
		</div>
	</div>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_tag')">标签<div class="tit_right">以英文小写逗号“,”分隔</div></div>
		<div class="admin_content">
			<div id="div_article_tag">
				<input name="article_tag" type="text" id="article_tag" size="40" class="input_text" value="<?php echo $article['tag'] ?>" />
			</div>
		</div>
	</div>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_alias')">别名<div class="tit_right">允许字母数字与“-”，禁止纯数字</div></div>
		<div class="admin_content">
			<div id="div_article_alias" style="display:none;">
				<input name="article_alias" type="text" id="article_alias" size="40" class="input_text" value="<?php echo $article['address'] ?>"/>
			</div>
		</div>
	</div>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_meta')">优化<div class="tit_right">有利于 SEO</div></div>
		<div class="admin_content">
			<div id="div_article_meta" style="display:none;">
				<p style="padding-bottom:5px;text-align:left;">关键字：</p>
				<input name="article_keywords" type="text" id="article_keywords" size="40" class="input_text" value="<?php echo $article['keywords'] ?>" />
				<p style="padding-bottom:5px;padding-top:5px;text-align:left;">描述：</p>
				<textarea name="article_description" type="text" id="article_description" style="padding:5px;width:235px;height:100px;font-size: 14px;border:1px solid #c8c8c8;"><?php echo $article['description'] ?></textarea>
			</div>
		</div>
	</div>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_type')">状态<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_type" style="display:none;">
				<input type="radio" value="公开" name="article_type" checked="checked" class="input_radio" />公开&nbsp;&nbsp;
				<input type="radio" value="隐藏" name="article_type" class="input_radio" />隐藏&nbsp;&nbsp;
				<input type="radio" value="草稿" name="article_type" class="input_radio" />草稿
			</div>
		</div>
	</div>

	<div class="admin_panel">
		<div class="admin_title" onclick="javascript:ShowHideDiv('div_article_trackback')">引用通告<div class="tit_right"></div></div>
		<div class="admin_content">
			<div id="div_article_trackback" style="display:none;">
				<input name="article_trackback" type="text" id="article_trackback" size="40" class="input_text" value="<?php echo $article['trackback'] ?>" />
			</div>
		</div>
	</div>

</div>

<div class="fixed"></div>

</form>

<script>
	$(document).ready(function(){
		$('#ajax_form').ajaxForm(function(data){
			showMessage(data);
			$("#admin_loading").css('visibility','hidden');
		});
	});
</script>

<?php
	}

	private function article_list() {
		global $db,$cache,$request;
		$current_page = max( 1, $request->page );
		$max_per_page = 20;
		$offset = ($current_page - 1) * $max_per_page;
		$article_num = $cache->count['article_num'];
		$sql = "SELECT id,author,title,time,istop,isdel,category,`read`,comment,`type` FROM `" . DB_PREFIX . "article` WHERE isdel=0 AND `type`<3 ORDER BY istop DESC, id DESC LIMIT $offset, $max_per_page";
		$article_array = $db->fetch_all( $sql );
?>
<div class="admin_title"><?php echo L('_ARTICLE_LIST_'); ?></div>
<div class="admin_content">
	<form name="smslist" action="admin_article.php?action=batch" method="post">
		<div style="padding:5px;margin:2px;">      
			<table class="pickme" style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
				<thead>
					<tr align="center" height="22" bgcolor="#ffffff">
						<td width="3%"><input type="checkbox" name="allbox" onclick="CheckAll();"><input type="checkbox" id="ckbox" style="display:none;" disabled></td>
						<td width="4%"><?php echo L('_NUM_'); ?></td>
						<td width="10%"><?php echo L('_AUTHOR_'); ?></td>
						<td width="32%"><?php echo L('_TITLE_'); ?></td>
						<td width="6%"><?php echo L('_TYPE_'); ?></td>
						<td width="14%"><?php echo L('_CATEGORY_'); ?></td>
						<td width="7%"><?php echo L('_READ_'); ?></td>
						<td width="7%"><?php echo L('_COMMENT_'); ?></td>
						<td width="11%"><?php echo L('_TIME_'); ?></td>
						<td width="6%"><?php echo L('_OPERATION_'); ?></td>
					</tr>
				</thead>
				<tbody>
					<?php
					$i = 1;
					foreach( $article_array as $value ) {
					?>
					<tr align="center" height="20" bgcolor="#ffffff">
						<td ><input type="checkbox" id="ckbox" name="id_arr[]" value="<?php echo $value['id'] ?>" /></td>
						<td><?php echo ( $current_page - 1 ) * $max_per_page + $i ?></td>
						<td><?php echo $value['author'] ?></td>
						<td style="text-align:left;padding-left:3px;"><?php if( $value['istop'] ) { echo '<span class="top_article">[置顶]</span>' . cut_str( $value['title'],40 ); }else{ echo cut_str( $value['title'],40 ); } ?></td>
						<td>
						<?php
							if( $value['type'] == 0 ) {
								echo '公开';
							} elseif( $value['type'] == 1 ) {
								echo '隐藏';
							} elseif( $value['type'] == 3 ) {
								echo '草稿';
							} else {
								echo 'Unknown';
							}
						?></td>
						<td><?php echo $cache->category[$value['category']]['name'] ?></td>
						<td><?php echo $value['read'] ?></td>
						<td><?php echo $value['comment'] ?></td>
						<td><?php echo date( 'Y-m-d', $value['time'] ) ?></td>
						<td>
							<span class="Control">
							<a href="#" onclick="ajax_load('<?php echo ADMIN_PATH ?>','article','edit',null,'<?php echo $value['id'] ?>');return false;"><?php echo L('_EDIT_'); ?></a>
							</span>
						</td>
					</tr>
					<?php
						$i ++;
					}
					?>
				</tbody>
			</table>
		</div>
		<?php echo Admin::get_navigator( $article_num, $max_per_page, $current_page, 'article', 'list' ); ?>
		<div align="center" style="margin-top:10px;">
			<?php echo L('_BATCH_ADMIN_'); ?>: 
			<input type="radio" name="Control" value="删除" checked="checked"/><?php echo L('_DELETE_'); ?> 
			<input type="radio" name="Control" value="移动" /><?php echo L('_MOVE_'); ?>
			<select name="article_category" id="article_category">
			<?php
				foreach ( $cache->category as $value ) {
					echo '<option value="' . $value['cid'] . '">' . $value['name'] . '</option>';
				}
			?>
			</select> 
			<input type="radio" name="Control" value="转换" /><?php echo L('_CHANGE_'); ?> 
			<select name="article_type" id="article_type">
				<option value="公开">公开</option>
				<option value="隐藏">隐藏</option>
				<option value="草稿">草稿</option>
			</select> 
			<input type="submit" class="main_button" value=" <?php echo L('_TAKE_OPERATION_'); ?> " />
		</div>
	</form>
	<div class="main_button"></div>
</div>
<?php
	}

	private function page_control() {
?>
<div class="admin_title"><?php echo L('_HTML_MANAGEMENT_'); ?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<a href="admin_article.php?action=MakearticlePage"><?php echo L('_REBUILD_HTML_'); ?></a>
	</div>
</div>
<?php
	}

	private function data_check() {
?>
<div class="admin_title"><?php echo L('_DATA_CHECK_'); ?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<a href="index.php?m=article&p=datacheckexe"><?php echo L('_COMMENT_DATA_CHECK_'); ?></a><br />
		<a href="index.php?m=article&p=tagdatacheckexe"><?php echo L('_TAG_DATA_CHECK_'); ?></a>
	</div>
</div>
<?php
	}

	private function main() {
?>
<div class="admin_title"><?php echo L('_CONTENT_MANAGEMENT_'); ?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<?php echo L('_UNDEFINED_ACTION_'); ?>
	</div>
</div>
<?php
	}

}
?>
